﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;

/// <summary>
/// Summary description for NetworkAdapterWs
/// </summary>
[WebService(Namespace = "http:/novakov.miecio.net/ServerDashborad/NetworkAdapter")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class NetworkAdapterWS : System.Web.Services.WebService {

    public NetworkAdapterWS () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    private void AddElement(XmlNode node, string name, string value)
    {
        XmlElement element = node.OwnerDocument.CreateElement(name);
        element.InnerXml = value;
        node.AppendChild(element);
    }

    [WebMethod]
    public string GetAdapter(string id)
    {
        var adapter=(new NetworkAdaptersWrapper()).GetAdapter(id);

        return new XElement("adapter",
            new XElement("connectionName", adapter.NetworkConnectionID),
            new XElement("connectionStatus", adapter.NetworkConnectionStatus.ToString()),
            new XElement("name", adapter.Name),
            new XElement("manufacturer", adapter.Manufacturer),
            new XElement("type", adapter.Type),
            new XElement("mac", adapter.MACAddress),
            new XElement("addresses", from ip in adapter.Addresses
                                      select new XElement("address",
                                                          new XElement("ip", ip.Key.ToString()),
                                                          new XElement("mask", ip.Value.ToString()))),
           new XElement("dnsServers", from dns in adapter.DNSServers
                                     select new XElement("dns", dns.ToString())),
           new XElement("gateways", from gateway in adapter.Gateway
                                    select new XElement("gateway", gateway.ToString())),
           new XElement("dhcpEnabled", adapter.DHCPEnabled.ToString()),
           new XElement("dhcpServer", adapter.DHCPServer),
           new XElement("dhcpObtained", adapter.DHCPEnabled ? adapter.DHCPLeaseObtained.ToString() : ""),
           new XElement("dhcpExpires", adapter.DHCPEnabled ? adapter.DHCPLeaseExpires.ToString() : "")).ToString();
    }
    
}

